// sharing/pages/index/index.js
var app = getApp();
Page({
	TOAPPID: require('../../resource/components/footer/footer.js'),
  data:{
	  wifiList: [],
	  wifiOpen: false,
	  shadeHide: true,
	  zzcz: true,
	  wifiIndex: 0,
	  psw: '',
	  list: [],
	  num: 0,
	  photoType: 0,
	  res: app.siteInfo.resource,
	  showPic: true,
  },
  onLoad: function(){
	  var that = this;
	  app.footerInit(that);
	  app.getShareData(function(res){
      if (res.data.clipboard) {
        wx.setClipboardData({
          data: res.data.clipboard,
        })
      }
		  that.setData({cfg: res.data})
      if(res.data.title){
        wx.setNavigationBarTitle({
          title: res.data.title ? res.data.title : '附近WIFI',
        })
      }
	  });
	  wx.getSystemInfo({
		  success: function(res){
		    if(res.platform == 'ios'){
		    	that.setData({photoType: 1,wifiOpen: true});
		    }
		  }
	  });
  },
  restart:function(t){
    var that = this;
	var photoType = that.data.photoType;
	var showPic = that.data.showPic;
	if(photoType!=0){
		//ios下拉刷新返回
		if(t==1){
			wx.getConnectedWifi({
        		success: function(res){
        			if(res.errCode==0 || res.errMsg == 'getConnectedWifi:ok'){
        				that.setData({wifi:res.wifi});
        			}else{
        				that.setData({wifi:false});
        			}
        		}
        	});
			return ;
		}else if(t == 2){
			var result = app.getWiFiSto(that,function(wifiList){
				that.getClearWiFi(wifiList);
			});
			if(result){
				that.setData({showPic: false});
			}
			return ;
		}
		
		if(!showPic){
			that.setData({showPic: true});
			return;
		}
	}
	wx.showLoading();
	//重新运行获取WiFi列表
	wx.getWifiList({
		success: function(res){
			if(photoType==0){
				if(res.errCode==0){
					that.setData({wifiOpen: true});
				}else if(res.errCode == 12006){
          that.setData({ wifiOpen: false ,needGps:true });
        }else{
					that.setData({wifiOpen: false});
				}
			}
		},
		fail: function(res){
			wx.hideLoading();
			if(photoType==0){
        if (res.errCode == 12006) {
          that.setData({ wifiOpen: false, needGps: true });
        }else that.setData({wifiOpen: false});
			}
		}
	});
  },
  onShow: function(){
	  //安卓显示页面自动刷新列表
	  var photoType = this.data.photoType;
	  var that = this;
	//初始化wifi模块
    wx.startWifi({
        success: function (r) {
        	//获取已连接wifi
        	wx.getConnectedWifi({
        		success: function(res){
        			if(res.errCode==0 || res.errMsg == 'getConnectedWifi:ok'){
        				that.setData({wifi:res.wifi});
        			}else{
        				that.setData({wifi:false});
        			}
        		}
        	});
        	//监听设置WiFi列表
        	wx.onGetWifiList(function (res) {
        		for(var i in res.wifiList){
        			if(photoType==1){
        				res.wifiList[i].signalStrength = res.wifiList[i].signalStrength * 100;
        			}
        			if(res.wifiList[i].signalStrength >= 75) res.wifiList[i].strength = 0;
        			else if(res.wifiList[i].signalStrength >= 50) res.wifiList[i].strength = 1;
        			else if(res.wifiList[i].signalStrength >= 25) res.wifiList[i].strength = 2;
        			else res.wifiList[i].strength = 3;
        		}
        		that.getClearWiFi(res.wifiList);
        		wx.hideLoading();
        		if(photoType==1){
        			var timestamp=new Date().getTime();
        			wx.setStorageSync('timestamp',timestamp);
        			wx.setStorageSync('wifiList',res.wifiList);
        		}
        		that.setData({showPic: false, wifiList:res.wifiList});
        	});
        	
        },
        fail: function (r) {
          console.log(r);
        }
      })
	  that.restart(2);
  },
  getClearWiFi: function(wifiList){
	var that = this;
	var bssids = '';
	for(var i in wifiList){
		if(bssids) bssids += ',' + wifiList[i].BSSID;
		else bssids = wifiList[i].BSSID;
	}
	app.util.request({
		url: 'entry/wxapp/getwifi',
		data: {
			m: 'junsion_wifinav',
			t: 1,
			bssids: bssids
		},
		success: function (res) {
			if(res.data.code == 1){
			    that.setData({num: res.data.num, list: res.data.list});
          if(res.data.adv){
              wx.getSystemInfo({
                success: function (s) {
                  that.setData({ adv: res.data.adv, advH: s.windowWidth / res.data.adv.width * res.data.adv.height * 2 });
                }
              })
          }
			}
		},
	})  
  },
  selButton: function(e){
	  this.setData({idx: e.currentTarget.dataset.idx});
  },
  clickWifi: function(e){
	  this.setData({formId: e.detail.formId});
	  this.confirmWiFi();
  },
  confirmWiFi: function(){
	  var idx = this.data.idx;
	  var formId = this.data.formId;
	  var wifiList = this.data.wifiList;
	  var list = this.data.list;
	  this.setData({wifiIndex: idx, psw: ''});
	  if(wifiList[idx].secure){
		  if(list[wifiList[idx].BSSID]){
			  this.setData({psw: list[wifiList[idx].BSSID].password});
			  this.linkWifi();
		  }else{
			  this.setData({shadeHide: false});
		  }
	  }else{
		  this.linkWifi();
	  }
  },
  onImg: function () {
    var that = this;
    if (that.data.adv.appid) {
      wx.navigateToMiniProgram({
        appId: that.data.adv.appid,
        path: that.data.adv.path,
      });
    } else if (that.data.adv.path) {
      wx.setStorageSync('advurl', that.data.adv.path);
      wx.navigateTo({
        url: '../webview/webview',
      });
    } else {
      wx.previewImage({
        urls: [that.data.adv.index],
      })
    }
  },
  cancel: function(){
	  this.setData({shadeHide: true});
  },
  confirm: function(){
	  if(!this.data.psw){
		  wx.showModal({
			  title: '温馨提示',
			  content: '请输入WIFI密码',
			  showCancel: false
		  });
		  return ;
	  }
	  this.setData({shadeHide: true});
	  this.linkWifi();
  },
  chang_pwd_bind: function(e){
	  this.setData({psw: e.detail.value});
  },
  linkWifi: function(){
	  var that = this;
	  var wifiList = that.data.wifiList;
	  var list = that.data.list;
	  var idx = that.data.wifiIndex;
	  var wifi = wifiList[idx];
	  var formId = this.data.formId;
	  //连接成功
	  wifi.password = that.data.psw;
    wifi.stitle = list[wifi.BSSID]?list[wifi.BSSID].stitle:'';
	  wifi.uncertain = 0;
	  wifi.id = 0;
	  if(formId) wifi.formId = formId;
	  if(list[wifi.BSSID] && list[wifi.BSSID].uncertain==1){
		  wifi.uncertain = 1;
		  wifi.id = list[wifi.BSSID].id;
	  }
	  that.setData({psw: ''});
	  wx.setStorageSync('wifi',wifi);
	  wx.navigateTo({url: '../frame/frame'})
  },
  
  onShareAppMessage: function () {
	  return {
	      title: this.data.cfg.share.indexShare,
	      imageUrl: this.data.cfg.share.indexPic,
	      path: '/sharing/pages/index/index',
	      success: function(res) {
	        // 转发成功
	      },
	      fail: function(res) {
	        // 转发失败
	      }
	    }
  },
  onPullDownRefresh: function(){
	//获取已连接wifi
	  this.restart(1);
	  wx.stopPullDownRefresh();
  }
})
